package com.demo.springcloud.run;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

@Component
public class BalenceClient{

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;

//    @Scheduled(cron = "0/1 * * * * ?")
    public void balenceCall(){
        long lnow = System.currentTimeMillis();
        ServiceInstance serviceInstance = loadBalancerClient.choose("MODULE-01");
        String result = restTemplate.getForObject("http://MODULE-01/balence",String.class);
//        String result = restTemplate.getForObject(String.format("http://%s:%d/balence",
//                serviceInstance.getHost(),serviceInstance.getPort()),String.class);
        System.out.println(result+",spend : "+(System.currentTimeMillis() - lnow));
    }


}
